package br.inf.solutions.apresentacoes.ssp.control.validator;

import java.util.Date;
import java.util.Map;

import br.com.vexillum.util.DateCalc;
import br.com.vexillum.util.Return;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.BehaviorChange;
import br.inf.solutions.apresentacoes.ssp.model.enums.Behavior;

public class BehaviorChangeValidator extends BaseValidator {

	public BehaviorChangeValidator(Map<String, Object> mapData) {
		super(mapData);
	}
	
	@Override
	public Return validateSave() {
		Return ret = super.validateSave();
		if(ret.isValid())
			ret.concat(validateDateInitial());
			ret.concat(existsBehavior());
		return ret;
	}
	
	private Return existsBehavior(){
		Return ret = new Return(true);
		BehaviorChange behavior = (BehaviorChange) entity;
		
		Behavior behavior1 = behavior.getPerson() == null ? null : behavior.getPerson().getBehavior();
		Behavior behavior2 = behavior.getBehaviorCurrent();
		
		if(behavior1 != null && behavior2 != null){
			if(behavior2.equals(behavior1)){
				ret.concat(creatReturn("behaviorCurrent", getValidationMessage(null, "behaviorcurrent", false)));
			}
		}
		
		
		return ret;
	}
	
	public Return validateDateInitial(){
		Return ret = new Return(true);
		BehaviorChange behavior = (BehaviorChange) entity;
		Date date1 = DateCalc.incrementDateInDays(behavior.getDateInitial(), 1);
		Date date2 = new Date();
		if(date2 != null){
			if(date1.before(date2)){
				ret.concat(creatReturn("dateInitial", getValidationMessage(null, "dateinitial", false)));
			}
		}
		return ret;
	}

}
